package com.csw.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


import java.io.IOException;

//一行有多个单词的mapreduce过程
public class Demo02WordCount {
    //map过程
    //创建静态类  继承Mapper类  给上输入输出形参
    public static class WordCountMap extends Mapper<LongWritable,Text,Text,LongWritable>{
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //Context承上启下  map中进行连接
            //每一次获取一行数据  以,进行拆分
            String line = value.toString();
            String[] split = line.split(",");
            for (String s : split) {
                context.write(new Text(s),new LongWritable(1));
            }

        }
    }
    //reduce过程
    public static class WordCountReduce extends Reducer<Text,LongWritable,Text,LongWritable>{
        //map中的多个数据同时来到reduce，在Iterable<LongWritable> values存储多个数据

        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
            Long sum=0l;//单词统计的结果
            //对Iterable<LongWritable> values进行增强for获取一个一个kv键值对
            //reduce之前shuffle过程中相同的k存储到同一个reduce中
            for (LongWritable value : values) {
                long l = value.get();
                sum+=l;
            }
            //输出结果获取当前单词
            String line = key.toString();
            //把结果输出到hdfs
            context.write(new Text(line),new LongWritable(sum));
        }
    }
    //启动mapreduce,并且连接这两个独立的任务
    public static void main(String[] args) throws Exception {
        //构建一个job
        Job job = Job.getInstance();
        //指定job任务名称(不指定也可以 为系统默认的名称)
        job.setJobName("wordcount1");
        //指定当前main所在类名
        job.setJarByClass(Demo02WordCount.class);
        //指定map类
        job.setMapperClass(WordCountMap.class);
        //指定map输出的k和v的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        //指定reduce类
        job.setReducerClass(WordCountReduce.class);
        //指定reduce输出的k和v的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        //获取fileSystem对象，hdfs文件管理系统的对象
        FileSystem fs = FileSystem.get(new Configuration());
        //指定输入路径
        FileInputFormat.addInputPath(job,new Path("/data/word.txt"));
        //指定输出路径(路径不能已存在，存在需要删除)
        Path path = new Path("/data/out1");
        if (fs.exists(path)){
            fs.delete(path,true);
        }
        //指定输出路径
        FileOutputFormat.setOutputPath(job,path);
        //启动
        job.waitForCompletion(true);
    }
}
